<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020-08-01
 * Time: 下午 07:33
 */
use Lib\Core;
use Lib\Injector;

error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);

require_once dirname(__DIR__) . '/core/lib/core.php';

Core::registerAutoLoad();

/**
 * sdsdsddsdsdsdds
 * Class ActionDispatcher
 * @property \Lib\Core $core
 * @property \Lib\Functions $fns
 */
class ActionDispatcher extends Injector {
    public function __construct() {
        $this->init();
    }

    public function init() {
        $this->phpConfig();
        $this->sessionInit();
    }
    
    public function sessionInit() {
        $sessionDir = $this->core->sessionsPath();
        
        if(!is_dir($sessionDir)) {
            mkdir($sessionDir, 0777, true);
        }
        
        session_save_path($sessionDir);
        session_start();
    }

    public function phpConfig() {
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('display_errors', 1);
    }
   
    public function dispatch() {
        $action = $_GET['a'];
        $action = strtolower($action);
        
        $action = str_replace('/', '', $action);
        $action = str_replace('\\', '', $action);
        $action = str_replace('.', '/', $action);
        
        if($this->fns->server->requestPath() != '/') {
            $action = 'index';
        }
        
        $this->core->action(empty($action) ? 'index' : $action);
    }
}

/** @var ActionDispatcher $dispatcher */
$dispatcher = di()->get(ActionDispatcher::class);
$dispatcher->dispatch();
